// lockbox.txt - This is the script you can place on a chest, desk, etc.
// to lock it. Then only a character with adequate Tool Use skill or
// the correct key could open it.

// Memory Cells - 
//   0 - Lock level. If left at 0, box is unlocked. Otherwise, the tool
//     use needed to get it open. If this is set to a really high number (say, 200),
//     it can't be unlocked by normal means.
//   1 - Key needed. If left at 0, no special item helps unlock the box. Otherwise,
//     if that party has this special item, the box automatically unlocks.
//   2,3 - Coordinates for a stuff done flag. If these are 0 and 0, ignored. Otherwise,
//     the stuff done flag is set to 1 when the door is unlocked. If the flag is non-zero,
//     than when the party enters this zone, the door will become unlocked.

beginterrainscript; 

variables;
	short cur_terrain;
	short i_am_locked = 0;
	
	short choice;
body;

beginstate INIT_STATE;
	if (get_memory_cell(0) > 0) {
		i_am_locked = 1;
		set_mechanism_difficulty(get_memory_cell(0));
		
		if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0)) {
			if (get_sdf(get_memory_cell(2),get_memory_cell(3)) > 0)
				i_am_locked = 0;
			}
		}
		
	break;

beginstate START_STATE;
break;

beginstate SEARCH_STATE;
	if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0)) {
		if (get_sdf(get_memory_cell(2),get_memory_cell(3)) > 0)
			i_am_locked = 0;
		}

	if (i_am_locked) {
		block_entry(1);
		
		if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0)) {
			if (get_sdf(get_memory_cell(2),get_memory_cell(3)) > 0)
				i_am_locked = 0;
			}
		
		if (i_am_locked) {
			print_str_color("Unlock Box: The container is locked.",2);
			if (get_memory_cell(1) > 0) {
				if (has_special_item(get_memory_cell(1)) > 0) {
					print_str_color("  One of your keys fits. You unlock the box.",2);
					i_am_locked = 0
					}
					else print_str_color("  You don't have the key that fits it.",2);
				}
			}
		
		if (i_am_locked) {
			if (run_pick_lock(get_mechanism_difficulty()) == FALSE) {
				print_str_color("  Nobody has high enough Tool Use skill to get it open.",2);
				end();
				}
			i_am_locked = 0;
			print_str_color("  You manage to pick the lock.",2);
			if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0)) 
				award_party_xp(BASE_TRAP_XP,1 + 2 * get_mechanism_difficulty());
			}
			
		if (i_am_locked == 0) {
			if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0))
				set_flag(get_memory_cell(2),get_memory_cell(3),1);
			block_entry(0);
			play_sound(-58);
			}
		}
break;

beginstate UNLOCK_SPELL_STATE;
	if (i_am_locked)
		print_str_color("Unlock Doors: The spell doesn't affect locked containers.",2);
break;

beginstate DISPEL_BARRIER_STATE;
	if (i_am_locked) {
		print_str_color("Dispel Barrier: The spell fails to affect a locked containers.",2);
		}
break;
